package com.servlet;

import com.dao.Impl.RoleDaoImpl;
import com.dao.Impl.StaffDaoImpl;
import com.entity.PageBean;
import com.entity.Role;
import com.entity.Staff;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.DoubleToIntFunction;

@WebServlet(value = "/staffController/*")
public class StaffManageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methon = MyUtil.getMethon(request);
        switch (methon) {
            case "show":
                selectStaff(request, response);
                break;
            case "update":
                updateStaff(request, response);
                break;
            case "add":
                insertStaff(request, response);
                break;
            case "delete":
                deleteStaff(request, response);
                break;
            case "selectId":
                selectStaffId(request, response);
                break;
            case "updatePass":
                updatePass(request, response);
                break;
            case "findinfo":
                findinfo(request, response);
                break;
            default:
                request.getRequestDispatcher("/staffManage/staff.jsp").forward(request, response);
                break;
        }
    }

    private void findinfo(HttpServletRequest request, HttpServletResponse response) {
        MyUtil.jsonWriter(response,new StaffDaoImpl().maxId()+1);
    }

    private void selectStaffId(HttpServletRequest request, HttpServletResponse response) {
        Integer staffid = Integer.valueOf(request.getParameter("staffid"));
        Staff staff = new StaffDaoImpl().findStaffId(staffid);
        MyUtil.jsonWriter(response, staff);
    }

    /**
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private void selectStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageBean<Staff> pageBean = new PageBean<>();
        TransBean.populate(pageBean, request.getParameterMap());
        new StaffDaoImpl().findAllStaff(pageBean);
        pageBean.setStartIndex();
        List<Role> roles = new RoleDaoImpl().findAll();
        Map map = new HashMap();
        map.put("staffs", pageBean);
        map.put("roles", roles);
        MyUtil.jsonWriter(response, map);
    }

    /**
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private void updateStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Staff staff = new Staff();
        TransBean.populate(staff, map);
        int pd = new StaffDaoImpl().update(staff);
        MyUtil.jsonWriter(response, pd);
    }

    /**
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private void deleteStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        int i = new StaffDaoImpl().delect(id);
        MyUtil.jsonWriter(response, i);
    }

    /***
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private void insertStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Staff staff = new Staff();
        TransBean.populate(staff, map);
        StaffDaoImpl dao = new StaffDaoImpl();
        int id = dao.insert(staff);
        dao.updatestaffId(id + 20200000, id);
        MyUtil.jsonWriter(response, id);
    }

    private void updatePass(HttpServletRequest request, HttpServletResponse response) {
        Staff staff = new Staff();
        TransBean.populate(staff, request.getParameterMap());
        int i = new StaffDaoImpl().updatePass(staff);
        MyUtil.jsonWriter(response, i);
    }
}
